package com.wen.cms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.cms.entity.CrmBanner;
import com.wen.cms.service.CrmBannerService;
import com.wen.commonutils.ResultEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * the admin banner management
 * </p>
 *
 * @author wen
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/edu/cms/service/banner/admin")
public class BannerAdminController {
    @Resource
    private CrmBannerService crmBannerService;
// TODO:the admin banner code to be done

    /**
     * page method for banner admin
     *
     * @param current current page
     * @param size    the number of pages to display
     * @return result of the operation
     */
    @GetMapping("pageBanner/{current}/{size}")
    public ResultEntity pageBanner(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        Page<CrmBanner> bannerPage = new Page<>(current, size);
        IPage<CrmBanner> page = crmBannerService.page(bannerPage, null);
        return ResultEntity.ok().data("items", page.getRecords()).data("total", page.getTotal());
    }

    /**
     * add banner
     *
     * @param crmBanner the banner to be added
     * @return result of the operation
     */
    @PostMapping("addBanner")
    public ResultEntity addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return ResultEntity.ok();
    }

    /**
     * delete banner
     *
     * @param crmBanner the banner to be deleted
     * @return result of the operation
     */
    @DeleteMapping("deleteBanner")
    public ResultEntity deleteBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.removeById(crmBanner);
        return ResultEntity.ok();
    }

    /**
     * get banner
     *
     * @param id the banner to be get
     * @return result of the operation
     */
    @GetMapping("getBanner/{id}")
    public ResultEntity getBanner(@PathVariable("id") String id) {
        CrmBanner crmBanner = crmBannerService.getById(id);
        return ResultEntity.ok().data("item", crmBanner);
    }

    /**
     * update banner
     *
     * @param crmBanner the banner to be updated
     * @return result of the operation
     */
    @GetMapping("updateBanner")
    public ResultEntity updateBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return ResultEntity.ok();
    }
}

